/*
* @Author: An
* @Date:   2017-07-08 14:08:40
* @Last Modified by:   An
* @Last Modified time: 2017-07-08 15:11:20
*/

'use strict';
require('./index.css');
require('page/common/nav-simple/index.css');
var _user = require('service/user-service.js');
var _xj = require('util/xj.js');

// 表单中的错误提示
var formError = {
	show : function(errMsg) {
		$('.error-item').show().find('.error-msg').text(errMsg);
	},
	hide : function() {
		$('.error-item').hide().find('.error-msg').text('');
	}
};

// 逻辑
var page = {
	data : {
		username 	: '',
		question 	: '',
		answer 		: '',
		token 		: ''
	},
	init : function() {
		this.onLoad();
		this.bindEvent();
	},
	onLoad : function(){
		this.loadStepUsername();
	},
	bindEvent : function(){
		var _this = this;
		// 输入用户名后下一步的点击
		$('#submit-username').click(function() {
			var username = $.trim($('#username').val());
			if (username) {
				_user.getQuestion(username, function(res) {
					_this.data.username = username;
					_this.data.question = res;
					_this.loadStepQuestion();
				}, function(errMsg) {
					formError.show(errMsg);
				});
			} else {
				formError.show("请输入用户名");
			}
		});
		// 输入密码提示问题答案的点击
		$('#submit-question').click(function() {
			var answer = $.trim($('#answer').val());
			if (answer) {
				_user.checkAnswer({
					username : _this.data.username,
					question : _this.data.question,
					answer : answer
				}, function(res) {
					_this.data.answer = answer;
					_this.data.token = res;
					_this.loadStepPassword();
				}, function(errMsg) {
					formError.show(errMsg);
				});
			} else {
				formError.show("请输入密码提示问题的答案");
			}
		});
		// 输入新密码后的点击
		$('#submit-password').click(function() {
			var password = $.trim($('#password').val());
			if (password && password.length >=6) {
				_user.resetPassword({
					username : _this.data.username,
					passwordNew : password,
					forgetToken : _this.data.token
				}, function(res) {
					window.location.href = './result.html?type=pass-reset';
				}, function(errMsg) {
					formError.show(errMsg);
				});
			} else {
				formError.show("请输入不少于6位的新密码");
			}
		});
	},
	// 加载找回密码第一步
	loadStepUsername : function() {
		$('.step-username').show();
	},
	// 加载找回密码第二步
	loadStepQuestion : function() {
		formError.hide();
		$('.step-username').hide().siblings('.step-question').show()
			.find('.question').text(this.data.question);
	},
	// 加载找回密码第三步
	loadStepPassword : function() {
		formError.hide();
		$('.step-question').hide().siblings('.step-password').show();
	}
};
$(function(){
	page.init();
});